package cn.cf.kxx.tools;

import android.util.Log;

import com.serotonin.modbus4j.serial.SerialPortWrapper;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

public class AndroidWrapper implements SerialPortWrapper {

    String serial;

    int baudRate = 115200;

    SerialPort serialPort;
    public AndroidWrapper(String serial) {
        this.serial = serial;
    }

    @Override
    public void close() throws Exception {
        if(serialPort != null){
            serialPort.close();
            serialPort = null;
        }
    }

    @Override
    public void open() throws Exception {
        serialPort = new SerialPort(new File(serial),baudRate,0);
        Log.e("dong","open..."+serialPort.stringFromJNI());
    }

    @Override
    public InputStream getInputStream() {
        Log.e("dong","getInputStream...");
        return serialPort.getInputStream();
    }

    @Override
    public OutputStream getOutputStream() {
        Log.e("dong","getOutputStream...");
        return serialPort.getOutputStream();
    }

    @Override
    public int getBaudRate() {
        return baudRate;
    }

    @Override
    public int getDataBits() {
        return 8;
    }

    @Override
    public int getStopBits() {
        return 1;
    }

    @Override
    public int getParity() {
        return 0;
    }
}
